<template>
  <div>
    <eagle-dialog-page
      :url="url"
      :params="deparmentParams"
      @initList="_initList"
      @open="open"
      @submit="submit"
      @callbackDialog="callbackDialog"
      @setData="setData"
      v-bind="$attrs"
      v-on="$listeners"
      ref="eagleDialogPage"
      :option="option"
      :title="title"
      :selectedValue="selectedValue"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
      >
        <div class="user-item">
          <div>
            <div class="mb-5 color-gray1">{{ item.OrgName }}</div>
            <div class="color-gray3">
              {{ item.OrgFullName }}
            </div>
          </div>
          
          <eagle-checkbox
            v-if="$attrs.isMulti ? $attrs.isMulti : false"
            :name="item.OrgCode"
            shape="round"
          />
          <eagle-radio
            v-else
            :name="item.OrgCode"
            @click="radioClick(item)"
          ></eagle-radio>
        </div>
      </van-cell>

      <template #seletedSlot>
        <van-cell
          name="seletedSlot"
          v-for="(item, index) in multiselectedlist"
          :key="index"
        >
          <div class="risk_item_buton">
            <div class="mb-5">
              <div class="mb-5">{{ item.OrgName }}</div>
              <div>
                {{ item.OrgFullName }}
              </div>
            </div>
            <van-icon
              name="delete"
              @click="deleteSelect(item.OrgCode)"
            />
          </div>
        </van-cell>
      </template>
    </eagle-dialog-page>
  </div>
</template>
<script>
export default {
  name: "department-choose-dialog",
  props: {
    title: {
      type: String,
      default: "选择部门",
    },
    selectedValue: {
      type: String,
      default: () => "",
    },
    url: {
      type: String,
      default: "/UserUser/GetChooseOrgPageData",
    },
  },
  data() {
    return {
      list: [],
      deparmentParams: [{}],
      selectedData: [],
      selecteCode: [],
      result: "",
      option: { name: "OrgName", code: "OrgCode" },
      simSelectObject: { code: "", name: "" },
      multiselectedlist: [],
      orgRowIndex: -1,
    };
  },
  created() {},
  computed: {
    multi: {
      get() {
        if (this.$attrs.isMulti) {
          return this.$attrs.isMulti;
        } else {
          return false;
        }
      },
    },
  },
  methods: {
    callbackDialog(data) {
      if (!data || data.length == 0) {
        this.$toast("您还未选择部门");
        return false;
      }
      if (this.multi) {
        this.$emit("callback", data);
      } else {
        this.$emit("callback", data);
      }
    },
    _initList(list) {
      
      if (
        this.$attrs.isOpenNew ||
        (this.$attrs.rowIndex && this.$attrs.rowIndex != this.orgRowIndex)
      ) {
        this.orgRowIndex = this.$attrs.rowIndex;
        this.multiselectedlist = [];
      }
      this.list = list;
      this.selecteCode = [];
      this.multiselectedlist = [];
      var newData = [];
      if (!this.multi) {
        newData = [{ code: this.selectedValue, OrgCode: this.selectedValue }];
      } else {
        var data1 = [];
        var data2 = [];
        var tagData = [];

        if (this.selectedValue) {
          data1 = this.selectedValue.split(",");
          data1.forEach((item1) => {
            data2 = list.filter((item) => item.OrgCode == item1);
            tagData.push(data2);
          });
          if (tagData.length > 0) {
            for (var i = 0; i < tagData.length; i++) {
              var innerItem = tagData[i];
              this.selecteCode.push(innerItem[0].OrgCode);
              newData.push({
                OrgCode: innerItem[0].OrgCode,
                OrgName: innerItem[0].OrgName,
                code: innerItem[0].OrgCode,
                name: innerItem[0].OrgName,
              });
            }
          }
        }
      }
      this.$refs.eagleDialogPage.initSeletedData(newData);
      this.multiselectedlist = newData;
    },
    open(list) {
      this.list = list;
    },
    submit() {
      this.$refs.eagleDialogPage.submit();
    },
    setData(data) {
      if (this.multi) {
        this.multiselectedlist = data;
      } else {
        this.simSelectObject = data;
      }
    },
    deleteSelect(code) {
      this.$refs.eagleDialogPage.deleteSelect(code);
    },
    clearOrgCode() {
      
      this.$refs.eagleDialogPage.getNoPageList();
    },
    radioClick(item) {
      let resultRadio = Object.assign({ code: "", name: "" }, item);
      resultRadio.code = item.OrgCode;
      resultRadio.name = item.OrgName;
      this.$emit("callback", resultRadio);
      this.$emit("update:show", false);
    },
  },
};
</script>
<style lang="less" scoped>
.user-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.risk_item_buton {
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>